﻿using Cinch;
using FR.SAFTAnalyser.Model;
using FR.SAFTAnalyser.ViewModels.Pane;
using MEFedMVVM.Common;
using System.Linq;

namespace FR.SAFTAnalyser.ViewModels
{
    public class CustomersViewModel : DocumentPartViewModel
    {
        public CustomersViewModel()
            : base("Clientes")
        {
            if (!Designer.IsInDesignMode)
            {
                Mediator.Instance.Register(this);
            }
        }

        internal override void ResetSaftFile()
        {
            this.Customers = null;
        }

        [MediatorMessageSink(MessageType.SAFT_FILE_OPENED)]
        private void LoadCustomers(AuditFile saft)
        {
            if (saft != null && saft.MasterFiles != null)
                this.Customers = saft.MasterFiles.Customer;
        }

        [MediatorMessageSink(MessageType.FOCUS_CUSTOMER)]
        internal void FocusCustomer(string pk)
        {
            if (!string.IsNullOrEmpty(pk) && Customers != null)
            {
                var customerWithError = from i in Customers
                                        where i.Pk.AreEqualIgnoreCase(pk)
                                        select i;

                if (customerWithError != null)
                    Customer = customerWithError.FirstOrDefault();
            }
        }

        Customer[] customers;
        public Customer[] Customers
        {
            get { return customers; }
            set
            {
                customers = value;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<CustomersViewModel>(x => x.Customers));
            }
        }

        Customer customer;
        public Customer Customer
        {
            get { return customer; }
            set
            {
                customer = value;
                NotifyPropertyChanged(ObservableHelper.CreateArgs<CustomersViewModel>(x => x.Customer));
            }
        }

        string filter;
        public string Filter
        {
            get { return filter; }
            set
            {
                filter = value;
                if (this.Customers != null)
                {
                    if (string.IsNullOrEmpty(filter))
                        this.Customers = SAFT.SaftAnalyser.Instance.SaftFile.MasterFiles.Customer;
                    else
                    {
                        this.Customers = (from c in SAFT.SaftAnalyser.Instance.SaftFile.MasterFiles.Customer
                                          where !string.IsNullOrEmpty(c.CompanyName) && c.CompanyName.IndexOf(filter, System.StringComparison.OrdinalIgnoreCase) >= 0 ||
                                             !string.IsNullOrEmpty(c.CustomerID) && c.CustomerID.IndexOf(filter, System.StringComparison.OrdinalIgnoreCase) >= 0 ||
                                             !string.IsNullOrEmpty(c.Email) && c.Email.IndexOf(filter, System.StringComparison.OrdinalIgnoreCase) >= 0 ||
                                             !string.IsNullOrEmpty(c.Telephone) && c.Telephone.IndexOf(filter, System.StringComparison.OrdinalIgnoreCase) >= 0 ||
                                             !string.IsNullOrEmpty(c.Website) && c.Website.IndexOf(filter, System.StringComparison.OrdinalIgnoreCase) >= 0 ||
                                             !string.IsNullOrEmpty(c.Contact) && c.Contact.IndexOf(filter, System.StringComparison.OrdinalIgnoreCase) >= 0
                                          select c).ToArray();
                    }
                }
            }
        }
    }
}
